﻿Imports System.Drawing

Public Class FishDetail
    Inherits ItemDetailPage

    Protected Property PhotoNormalSize() As New Size() With {.width = 256, .height = 256}
    Protected Property PhotoThumbSize() As New Size() With {.width = 128, .height = 128}
    Protected Property FishID() As ULong

    Protected Overrides ReadOnly Property InsertionPermitted() As Boolean
        Get
            Return Roles.IsUserInRole("Manager") Or Roles.IsUserInRole("Editor")
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ULong.TryParse(Request.QueryString.Get("ID"), FishID)

        If Not IsPostBack Then
            If Not FishID AndAlso Request.QueryString.Get("Action") = "Add" Then
                If InsertionPermitted Then
                    DetailFormView.DefaultMode = FormViewMode.Insert
                Else
                    StopEditing()
                End If
            ElseIf FishID AndAlso FishExists(FishID) Then
                If Request.QueryString.Get("Action") = "Edit" Then
                    If ModificationPermitted Then
                        DetailFormView.DefaultMode = FormViewMode.Edit
                        UpdateDataSource()
                    Else
                        StopEditing()
                    End If
                Else
                    DetailFormView.DefaultMode = FormViewMode.ReadOnly
                    UpdateDataSource()
                End If
            Else
                RedirectToList()
            End If
        End If
    End Sub

    Protected Sub SaveButton_Click(sender As Object, e As EventArgs)
        If ModificationPermitted Then
            Dim gateway As New FishGateway()
            Dim title As TextBox = DetailFormView.FindControl("TitleTextBox")
            Dim sciname As TextBox = DetailFormView.FindControl("SciNameTextBox")
            Dim description As TextBox = DetailFormView.FindControl("DescriptionTextBox")
            Dim diet As TextBox = DetailFormView.FindControl("DietTextBox")
            Dim minTankSize As TextBox = DetailFormView.FindControl("MinTankLengthTextBox")
            Dim maxLength As TextBox = DetailFormView.FindControl("MaxFishLengthTextBox")
            Dim waterPH As TextBox = DetailFormView.FindControl("phTextBox")
            Dim waterTemp As TextBox = DetailFormView.FindControl("WaterTempTextBox")
            Dim photo As FileUpload = DetailFormView.FindControl("PhotoFileUpload")
            If photo.HasFile() Then
                Dim fileName As String = PhotoUtils.GetUniqueFileName(photo.FileName)
                PhotoUtils.Save(fileName, photo.PostedFile.InputStream, PhotoNormalSize, PhotoThumbSize)
                gateway.Update(FishID, title.Text, sciname.Text, description.Text, diet.Text,
                               CDbl(maxLength.Text), CDbl(waterTemp.Text), CDbl(waterPH.Text),
                               CDbl(minTankSize.Text), CreatePhoto(title.Text, description.Text, fileName))
            Else
                gateway.Update(FishID, title.Text, sciname.Text, description.Text, diet.Text,
                               CDbl(maxLength.Text), CDbl(waterTemp.Text), CDbl(waterPH.Text),
                               CDbl(minTankSize.Text))
            End If
            StopEditing()
        End If
    End Sub

    Protected Sub CancelEditButton_Click(sender As Object, e As EventArgs)
        StopEditing()
    End Sub

    Protected Sub EditButton_Click(sender As Object, e As EventArgs)
        StartEditing()
    End Sub

    Protected Sub DeleteButton_Click(sender As Object, e As EventArgs)
        If ModificationPermitted Then
            Dim gateway As New FishGateway()
            gateway.DeleteByID(FishID)
            RedirectToList()
        End If
    End Sub

    Protected Sub SubmitButton_Click(sender As Object, e As EventArgs)
        If InsertionPermitted Then
            Dim gateway As New FishGateway()
            Dim title As TextBox = DetailFormView.FindControl("TitleTextBox")
            Dim sciname As TextBox = DetailFormView.FindControl("SciNameTextBox")
            Dim description As TextBox = DetailFormView.FindControl("DescriptionTextBox")
            Dim diet As TextBox = DetailFormView.FindControl("DietTextBox")
            Dim minTankSize As TextBox = DetailFormView.FindControl("MinTankLengthTextBox")
            Dim maxLength As TextBox = DetailFormView.FindControl("MaxFishLengthTextBox")
            Dim waterPH As TextBox = DetailFormView.FindControl("phTextBox")
            Dim waterTemp As TextBox = DetailFormView.FindControl("WaterTempTextBox")
            Dim photo As FileUpload = DetailFormView.FindControl("PhotoFileUpload")
            If photo.HasFile() Then
                Dim fileName As String = PhotoUtils.GetUniqueFileName(photo.FileName)
                PhotoUtils.Save(fileName, photo.PostedFile.InputStream, PhotoNormalSize, PhotoThumbSize)
                FishID = gateway.Insert(title.Text, sciname.Text, description.Text, diet.Text,
                                        CDbl(maxLength.Text), CDbl(waterTemp.Text), CDbl(waterPH.Text),
                                        CDbl(minTankSize.Text), GetCurrentUserID(),
                                        CreatePhoto(title.Text, description.Text, fileName))
            Else
                ' We do not normally expect this to occur! How can it be reproduced?
                Throw New HttpException(500, "500 Error - Internal Server Error")
            End If
            StopEditing()
        End If
    End Sub

    Protected Function FishExists(ByVal ID As ULong) As Boolean
        Dim gateway As New FishGateway()
        Return gateway.Exists(ID)
    End Function

    Protected Overrides ReadOnly Property UserHasOwnership() As Boolean
        Get
            Dim gateway As New FishGateway()
            Return gateway.HasOwnership(FishID, User.Identity.Name)
        End Get
    End Property

    Protected Sub StartEditing()
        Dim redirectUrl As String = String.Format("~/Pages/FishDetail.aspx?ID={0}&Action=Edit", FishID)
        Response.Redirect(redirectUrl)
    End Sub

    Protected Sub StopEditing()
        Dim redirectUrl As String = String.Format("~/Pages/FishDetail.aspx?ID={0}", FishID)
        Response.Redirect(redirectUrl)
    End Sub

    Protected Sub RedirectToList()
        Dim redirectUrl As String = String.Format("~/Pages/FishList.aspx")
        Response.Redirect(redirectUrl)
    End Sub

    Protected Sub UpdateDataSource()
        Dim gateway As New FishGateway()
        DetailFormView.DataSource = gateway.FindByID(FishID)
        DetailFormView.DataBind()
    End Sub

    Private Function CreatePhoto(ByVal title As String, ByVal description As String, ByVal filename As String) As ULong
        Dim gateway As New PhotoGateway()
        Return gateway.Insert(title, description, GetCurrentUserID(), filename)
    End Function

End Class